$(function(){
	var basePath=$("#basepath").val();
	$("#project_edit_form select[name='college']").change(function(){
		var collegeId=$(this).val();
		var $depSelect=$("#project_edit_form select[name='department']");
		$depSelect.html("");
		var $teacherSelect=$("#project_edit_form select[name='teacher']");
		$teacherSelect.html("<option value='0'></option>");
		$teacherSelect.val(0);
		$.get(basePath+ "admin/branch/getDepsOfCollege/"+collegeId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							$(" <option/>").val(data[i].id).html(data[i].name).appendTo($depSelect);
						}
						if(data.length==0){
							$(" <option value='0'>不可用</option>").appendTo($depSelect);
							makeAlert("无可用系,请添加之后再试","error",$("#project_edit_alert_container"),"no_dep_error");
						}else{
							$depSelect.change();
							$("#project_edit_alert_container .no_dep_error").alert('close');
						}
					}
			}, "json");
	});
	$("#project_edit_form select[name='department']").change(function(){
		var $teacherSelect=$("#project_edit_form select[name='teacher']");
		$teacherSelect.chosen({no_results_text: "没有找到教师:",search_contains:true}); 
		$teacherSelect.html("<option value='0'></option>");
		var orgTeaId=$teacherSelect.attr("data-org");
		$teacherSelect.attr("data-org","0");
		var depId=$(this).val();
		$.get(basePath+ "admin/teacher/getAllTeacherByDep/"+depId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							var $option=$("<option/>").html(data[i].name).attr("value",data[i].id);
							if(data[i].id==orgTeaId){
								$option.attr("selected","true");
							}
							$option.appendTo($teacherSelect);
						}
						$teacherSelect.val(orgTeaId);
						$teacherSelect.trigger('chosen:updated');//重新生成列表
						$teacherSelect.change();
					}
			}, "json");
		//专业方向 选择框
		var $branchSelect=$("#project_edit_form select[name='branche']");
		$branchSelect.chosen({no_results_text: "没有找到专业方向:",display_selected_options:false,search_contains:true}); 
		$branchSelect.html("<option value='0'></option>");
		var orgBranches=$branchSelect.parent().find(".orgBranches").html();
		$branchSelect.parent().find(".orgBranches").html("");
		var orgBranchArr=orgBranches.split(",");
		if(orgBranchArr.length>0){
			orgBranchArr.length-=1;
		}
		$.get(basePath+ "admin/branch/getAllBranchOfDep/"+depId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							var $option=$("<option/>").html(data[i].name).attr("value",data[i].id);
							for(j=0;j<orgBranchArr.length;j++){
								if(data[i].id==orgBranchArr[j]){
									$option.attr("selected","true");
									break;
								}
							}
							$option.appendTo($branchSelect);
						}
						$branchSelect.trigger('chosen:updated');//重新生成列表
					}
			}, "json");
	});
	$("#project_edit_form select[name='teacher']").change(function(){
		var $SYteacherSelect=$("#project_edit_form select[name='shengyue_teacher']");
		$SYteacherSelect.chosen({no_results_text: "没有找到教师:",search_contains:true}); 
		$SYteacherSelect.html("<option value='0'></option>");
		var orgSYTeaId=$SYteacherSelect.attr("data-org");
		$SYteacherSelect.attr("data-org","0");
		var depId=$("#project_edit_form select[name='department']").val();
		var teaId=$(this).val();
		$.get(basePath+ "admin/teacher/getAllTeacherByDep/"+depId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							var $option=$("<option/>").html(data[i].name).attr("value",data[i].id);
							if(teaId==data[i].id){
								continue;
							}
							if(data[i].id==orgSYTeaId){
								$option.attr("selected","true");
							}
							$option.appendTo($SYteacherSelect);
						}
						$SYteacherSelect.val(orgSYTeaId);
						$SYteacherSelect.trigger('chosen:updated');//重新生成列表
					}
			}, "json");
	});
	$("#project_edit_form select[name='college']").change();
	
	
	$("#project_edit_form_save").click(function(){
		var prjId=$("#project_edit_form input[name='project_id']").val();
		var prjName=$("#project_edit_form input[name='project_name']").val();
		var prjTeaid=$("#project_edit_form select[name='teacher']").val();
		var prjShengyueTeaid=$("#project_edit_form select[name='shengyue_teacher']").val();
		var prjDescription=$("#project_edit_form textarea[name='project_desc']").val();
		var prjBranches=$("#project_edit_form select[name='branche']").val();
		if(prjTeaid==0){
			makeAlert("请选择指导教师","error",$("#project_edit_alert_container"),"no_teacher");
			return;
		}else{
			$("#teacher_edit_alert_container .no_teacher").alert('close');
		}
		if(prjShengyueTeaid==0){
			makeAlert("请选择评阅人","error",$("#project_edit_alert_container"),"no_shengyue_teacher");
			return;
		}else{
			$("#teacher_edit_alert_container .no_shengyue_teacher").alert('close');
		}
		if(prjBranches==null||prjBranches.length==0){
			makeAlert("请至少选择一个适用专业","error",$("#project_edit_alert_container"),"no_branch");
			return;
		}else{
			$("#teacher_edit_alert_container .no_branch").alert('close');
		}
		$("#teacher_edit_alert_container .empty_field_error").alert('close');
		$("#teacher_edit_alert_container .no_branch").alert('close');
		$("#teacher_edit_alert_container .not_equal").alert('close');
		$.post(basePath+ "admin/project/updateProject",
				{'id':prjId,'name':prjName,'tea_id':prjTeaid,'shengyue_tea_id':prjShengyueTeaid,'description':prjDescription,'branches':prjBranches},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (data) {
						case "-1":
							makeAlert("带*字段不允许为空,请重新检查","error",$("#project_edit_alert_container"),"empty_field_error");
							break;
						case "-2":
							makeAlert("请至少选择一个适用专业","error",$("#project_edit_alert_container"),"no_branch");
							break;
						case "-3":
							makeAlert("指导教师和审阅教师须在同一个系,请重新检查","error",$("#project_edit_alert_container"),"not_equal");
							break;
						case "-4":
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case "0":
							noty({text:"更新成功,正在刷新"});
							window.location=document.referrer;
							break;
						}
					}
				},"text");
	});
	$(".project_file_delete").click(function(){
		var type=$(this).attr("data-type");
		var title=""
		$("#project_file_delete_box input[name='deleteType']").val(type);
		if(type==1){
			title="删除题目附件";
		}	
		else if(type==2){
			title="删除任务书";
		}	
		else if(type==3){
			title="删除最终毕业论文";
		}
		$("#project_file_delete_box .modal-header h3").html(title);
		$("#project_file_delete_box").modal("show");
	});
	$("#project_file_delete_box_delete").click(function(){
		var prjId=$("#project_edit_form input[name='project_id']").val();
		var type=$("#project_file_delete_box input[name='deleteType']").val();
		$.get(basePath+ "admin/project/deleteProjectFile/"+prjId+"/"+type,{},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (data) {
						case "-1":
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case "0":
							noty({text:"删除成功,正在刷新"});
							$("#project_file_delete_box").modal("hide");
							location.reload();
							break;
						}
					}
				},"text");
	});
	$(".project_file_upload").click(function(){
		var type=$(this).attr("data-type");
		var isNew=$(this).attr("data-isnew");
		var title=""
		$("#project_file_upload_box input[name='uploadType']").val(type);
		if(type==1&&isNew==1){
			title="上传题目附件";
		}else if(type==1&&isNew==0){
			title="重新选择题目附件";
		}else if(type==2&&isNew==1){
			title="上传任务书";
		}else if(type==2&&isNew==0){
			title="重新选择任务书";
		}else if(type==3&&isNew==1){
			title="上传毕业论文";
		}else  if(type==3&&isNew==0){
			title="重新选毕业论文";
		}
		$("#project_file_upload_box .modal-header h3").html(title);
		$("#project_file_upload_box_file_confirm").unbind();
		$("#project_file_upload_box_file_name").html("未选择");
		$("#project_file_upload_box_alert_container").html("");
		$("#project_file_upload_box_file").show();
		$("#project_file_upload_box_progress").hide();
		$("#project_file_upload_box_progress div").css("width","0%");
		$("#project_file_upload_box").modal("show");
	});
	$("#project_file_upload_box_file input[type='file']").fileupload({  
	    autoUpload:false,
	    done:function(e,result){  
			noty({text:"上传完成,正在刷新"});
			location.reload();
	    } ,
	    progress:function(e,result){
	    	var progress=parseInt(result.loaded / result.total * 100, 10);
	    	$("#project_file_upload_box_progress div").css("width",progress+"%");
	    },
	    add:function(e,result){
	    	var fileName=result.files[0].name.toLowerCase();
	    	$("#project_file_upload_box_file_name").html(fileName);
			$("#project_file_upload_box_file_confirm").unbind();
			$("#project_file_upload_box_file_confirm").click(function(){
				$("#project_file_upload_box_file_name").html("正在上传:"+fileName);
				$("#project_file_upload_box_file").hide();
				$("#project_file_upload_box_progress").show();
				var prjId=$("#project_edit_form input[name='project_id']").val();
				var type=$("#project_file_upload_box input[name='uploadType']").val();
				result.url=basePath+"admin/project/uploadProjectFile/"+prjId+"/"+type;
				result.submit();
			});
	    }
	}) ;
	
	$("#project_add_form select[name='college']").change(function(){
		var collegeId=$(this).val();
		var $depSelect=$("#project_add_form select[name='department']");
		$depSelect.html("");
		var $teacherSelect=$("#project_add_form select[name='teacher']");
		$teacherSelect.html("<option value='0'></option>");
		$teacherSelect.val(0);
		$.get(basePath+ "admin/branch/getDepsOfCollege/"+collegeId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							$(" <option/>").val(data[i].id).html(data[i].name).appendTo($depSelect);
						}
						if(data.length==0){
							$(" <option value='0'>不可用</option>").appendTo($depSelect);
							makeAlert("无可用系,请添加之后再试","error",$("#project_add_alert_container"),"no_dep_error");
						}else{
							$depSelect.change();
							$("#project_add_alert_container .no_dep_error").alert('close');
						}
					}
			}, "json");
	});
	$("#project_add_form select[name='department']").change(function(){
		var $teacherSelect=$("#project_add_form select[name='teacher']");
		$teacherSelect.chosen({no_results_text: "没有找到教师:",search_contains:true}); 
		$teacherSelect.html("<option value='0'></option>");
		var depId=$(this).val();
		$.get(basePath+ "admin/teacher/getAllTeacherByDep/"+depId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							var $option=$("<option/>").html(data[i].name).attr("value",data[i].id);
							$option.appendTo($teacherSelect);
						}
						$teacherSelect.trigger('chosen:updated');//重新生成列表
						$teacherSelect.change();
					}
			}, "json");
		//专业方向 选择框
		var $branchSelect=$("#project_add_form select[name='branche']");
		$branchSelect.chosen({no_results_text: "没有找到专业方向:",display_selected_options:false,search_contains:true}); 
		$branchSelect.html("<option value='0'></option>");
		$.get(basePath+ "admin/branch/getAllBranchOfDep/"+depId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							var $option=$("<option/>").html(data[i].name).attr("value",data[i].id);
							$option.appendTo($branchSelect);
						}
						$branchSelect.trigger('chosen:updated');//重新生成列表
					}
			}, "json");
	});
	$("#project_add_form select[name='teacher']").change(function(){
		var $SYteacherSelect=$("#project_add_form select[name='shengyue_teacher']");
		$SYteacherSelect.chosen({no_results_text: "没有找到教师:",search_contains:true}); 
		$SYteacherSelect.html("<option value='0'></option>");
		var depId=$("#project_add_form select[name='department']").val();
		var teaId=$(this).val();
		$.get(basePath+ "admin/teacher/getAllTeacherByDep/"+depId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							var $option=$("<option/>").html(data[i].name).attr("value",data[i].id);
							if(teaId==data[i].id){
								continue;
							}
							$option.appendTo($SYteacherSelect);
						}
						$SYteacherSelect.trigger('chosen:updated');//重新生成列表
					}
			}, "json");
	});
	$("#project_add_form select[name='college']").change();
	$("#project_add_form_save").click(function(){
		var prjName=$("#project_add_form input[name='project_name']").val();
		var prjTeaid=$("#project_add_form select[name='teacher']").val();
		var prjShengyueTeaid=$("#project_add_form select[name='shengyue_teacher']").val();
		var prjDescription=$("#project_add_form textarea[name='project_desc']").val();
		var prjBranches=$("#project_add_form select[name='branche']").val();
		if(prjTeaid==0){
			makeAlert("请选择指导教师","error",$("#project_add_alert_container"),"no_teacher");
			return;
		}else{
			$("#project_add_alert_container .no_teacher").alert('close');
		}
		if(prjShengyueTeaid==0){
			makeAlert("请选择评阅人","error",$("#project_add_alert_container"),"no_shengyue_teacher");
			return;
		}else{
			$("#project_add_alert_container .no_shengyue_teacher").alert('close');
		}
		if(prjBranches==null||prjBranches.length==0){
			makeAlert("请至少选择一个适用专业","error",$("#project_add_alert_container"),"no_branch");
			return;
		}else{
			$("#project_add_alert_container .no_branch").alert('close');
		}
		$("#project_add_alert_container .empty_field_error").alert('close');
		$("#project_add_alert_container .no_branch").alert('close');
		$("#project_add_alert_container .not_equal").alert('close');
		$.post(basePath+ "admin/project/insertProject",
				{'name':prjName,'tea_id':prjTeaid,'shengyue_tea_id':prjShengyueTeaid,'description':prjDescription,'branches':prjBranches},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (data) {
						case "-1":
							makeAlert("带*字段不允许为空,请重新检查","error",$("#project_add_alert_container"),"empty_field_error");
							break;
						case "-2":
							makeAlert("请至少选择一个适用专业","error",$("#project_add_alert_container"),"no_branch");
							break;
						case "-3":
							makeAlert("指导教师和审阅教师须在同一个系,请重新检查","error",$("#project_add_alert_container"),"not_equal");
							break;
						case "-4":
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case "0":
							noty({text:"更新成功,正在刷新"});
							window.location=document.referrer;
							break;
						}
					}
				},"text");
	});
});